﻿///////////////////////////////////////////////////
//项目：基于手机蓝牙的可配置的计算机应用程序遥控
//作者：华南理工：曹炼强 林伟彬
//本文件功能：解析、修改xml文件
//////////////////////////////////////////////////
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
using System.Windows.Forms;
using System.IO;

namespace BCC
{
    class xmlClass
    {
        private XmlDocument xmldoc = new XmlDocument();
        private XmlElement xmlelem;

        //创建一个xml文档
        public void creatXmldoc(string fileName)
        {
            XmlDeclaration xmldecl;
            xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
            xmldoc.AppendChild(xmldecl);
            xmlelem = xmldoc.CreateElement("", "myKeySet", "");//设置根结点
            xmldoc.AppendChild(xmlelem);
            string dirpath = Application.StartupPath + "\\data\\";
            fileName = dirpath + fileName;
            xmldoc.Save(fileName);
        }

        //添加一个节点
        //用于向xml文件中添加一个程序
        //添加其名称属性和路径属性并为其初始化17个按键信息
        public void addXmlNode(string fileName,string appName, string Path)
        {
            try
            {
                string dirpath = Application.StartupPath+ "\\data\\";
                fileName = dirpath + fileName;
                //xml文件路径
                xmldoc.Load(fileName);                              //加载xml文件
                XmlNode root = xmldoc.SelectSingleNode("myKeySet"); //选择<myKeySet>根结点
                XmlElement xe = xmldoc.CreateElement("Node");      //创建一个<Node>节点
                xe.SetAttribute("程序名称", appName);              //设置程序名称属性
                xe.SetAttribute("程序路径", Path);                 //设置程序路径属性
                root.AppendChild(xe);                              //添加到<mykeySet>节点中
                XmlElement xesub0 = xmldoc.CreateElement("zero");
                XmlElement xesub1 = xmldoc.CreateElement("one");
                XmlElement xesub2 = xmldoc.CreateElement("two");
                XmlElement xesub3 = xmldoc.CreateElement("three");
                XmlElement xesub4 = xmldoc.CreateElement("four");
                XmlElement xesub5 = xmldoc.CreateElement("five");
                XmlElement xesub6 = xmldoc.CreateElement("six");
                XmlElement xesub7 = xmldoc.CreateElement("seven");
                XmlElement xesub8 = xmldoc.CreateElement("eight");
                XmlElement xesub9 = xmldoc.CreateElement("nine");
                XmlElement xesub10 = xmldoc.CreateElement("ok");
                XmlElement xesub11= xmldoc.CreateElement("up");
                XmlElement xesub12= xmldoc.CreateElement("down");
                XmlElement xesub13= xmldoc.CreateElement("left");
                XmlElement xesub14= xmldoc.CreateElement("right");
                XmlElement xesub15= xmldoc.CreateElement("xing");
                XmlElement xesub16= xmldoc.CreateElement("jing");
                xe.AppendChild(xesub0);                            //添加到<Node>节点中
                xe.AppendChild(xesub1);
                xe.AppendChild(xesub2);
                xe.AppendChild(xesub3);
                xe.AppendChild(xesub4);
                xe.AppendChild(xesub5);
                xe.AppendChild(xesub6);
                xe.AppendChild(xesub7);
                xe.AppendChild(xesub8);
                xe.AppendChild(xesub9);
                xe.AppendChild(xesub10);
                xe.AppendChild(xesub11);
                xe.AppendChild(xesub12);
                xe.AppendChild(xesub13);
                xe.AppendChild(xesub14);
                xe.AppendChild(xesub15);
                xe.AppendChild(xesub16);
                xmldoc.Save(fileName);                              //保存创建好的XML文档
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        //更新节点属性
        //更新程序名称
        //根据传入的文件名、程序名找到对应的程序结点并更新其名称属性
        public void updateName(string fileName,string appName, string newName)
        {
            try
            {
                string dirpath = Application.StartupPath + "\\data\\";
                fileName = dirpath + fileName;
                xmldoc.Load(fileName);
                XmlNodeList nodeList = xmldoc.SelectSingleNode("myKeySet").ChildNodes;  //获取myKeySet节点的所有子结点
                foreach (XmlNode xn in nodeList)                                        //遍历子结点
                {
                    XmlElement xe = (XmlElement)xn;                                     //将XmlNode类型转换为XmlElement类型
                    if (xe.GetAttribute("程序名称") == appName)                         //判断程序名称属性值
                    {
                        xe.SetAttribute("程序名称", newName);                           //重设程序名称属性值
                    }
                }
                xmldoc.Save(fileName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        //添加结点元素
        //在程序中添加一个子结点
        //针对某个程序添加一个按键并设置其值
        public void addNodeElement(string fileName, string appName, string keyStr, string cmdStr)
        {
            try
            {
                string dirpath = Application.StartupPath + "\\data\\";
                fileName = dirpath + fileName;              
                xmldoc.Load(fileName);
                XmlNodeList nodeList = xmldoc.SelectSingleNode("myKeySet").ChildNodes;
                foreach (XmlNode xn in nodeList)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.GetAttribute("程序名称") == appName)
                    {
                        XmlElement xesub1 = xmldoc.CreateElement(keyStr);
                        xesub1.InnerText = cmdStr;                      //设置文本节点
                        xe.AppendChild(xesub1);                         //添加到<Node>节点中
                    }
                }
                xmldoc.Save(fileName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
       
        //更新程序中的按键设置
        //修改某个键对应的值
        public void updateNodeElement(string fileName, string appName, string keyStr, string cmdStr)
        {
            try
            {
                string dirpath = Application.StartupPath + "\\data\\";
                fileName = dirpath + fileName;
                xmldoc.Load(fileName);
                XmlNodeList nodeList = xmldoc.SelectSingleNode("myKeySet").ChildNodes;
                foreach (XmlNode xn in nodeList)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.GetAttribute("程序名称") == appName)
                    {
                        XmlNodeList nls = xe.ChildNodes;
                        foreach (XmlNode xn1 in nls)
                        {
                            XmlElement xe2 = (XmlElement)xn1;

                            if (xe2.Name == keyStr)//如果找到
                            {
                                xe2.InnerText = cmdStr;//修改该键对应的值
                            }
                        }
                    }
                }
                xmldoc.Save(fileName);//保存。  
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        //删除结点
        //从xml文件中删除一个程序对应的结点及其所有子结点
        public void deletXmlNode(string fileName, string appName)
        {
            try
            {
                string dirpath = Application.StartupPath + "\\data\\";
                fileName = dirpath + fileName;
                xmldoc.Load(fileName);
                XmlNode root = xmldoc.SelectSingleNode("myKeySet");
                XmlNodeList xnl = xmldoc.SelectSingleNode("myKeySet").ChildNodes;
                for (int i = 0; i < xnl.Count; i++)
                {
                    XmlElement xe = (XmlElement)xnl.Item(i);
                    if (xe.GetAttribute("程序名称") == appName)
                    {
                        root.RemoveChild(xe);
                        if (i < xnl.Count) i = i - 1;
                    }
                }
                xmldoc.Save(fileName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        //搜索结点
        //搜索xml文件中是否已经对一个程序进行了配置
        //若已经存在该程序的配置信息返回true否则返回false
        public bool searchNode(string fileName, string appName)
        {
            try
            {
                string dirpath = Application.StartupPath + "\\data\\";
                fileName = dirpath + fileName;
                xmldoc.Load(fileName);
                XmlNode root = xmldoc.SelectSingleNode("myKeySet");
                XmlNodeList xnl = xmldoc.SelectSingleNode("myKeySet").ChildNodes;
                for (int i = 0; i < xnl.Count; i++)
                {
                    XmlElement xe = (XmlElement)xnl.Item(i);
                    if (xe.GetAttribute("程序名称") == appName)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }

        //搜索结点元素
        //搜索具体一个应用程序的某个按键是否已经配置
        //如存在该个按键的配置信息返回true否则返回false
        public bool searchNodeElement(string fileName, string appName, string keyStr)
        {
            try
            {
                string dirpath = Application.StartupPath + "\\data\\";
                fileName = dirpath + fileName;
                xmldoc.Load(fileName);
                XmlNodeList xnl = xmldoc.SelectSingleNode("myKeySet").ChildNodes;
                foreach (XmlNode xn in xnl)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.GetAttribute("程序名称") == appName)
                    {
                        XmlNodeList nls = xe.ChildNodes;
                        foreach (XmlNode xn1 in nls)
                        {
                            XmlElement xe2 = (XmlElement)xn1;
                            if (xe2.Name == keyStr)
                            {
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }

        //查找程序结点
        //返回该xml文件中红所有配置应用程序的名字
        public string findApp(string fileName)
        {
            try
            {
                string str = "";
                string dirpath = Application.StartupPath + "\\data\\";
                fileName = dirpath + fileName;
                xmldoc.Load(fileName);
                XmlNodeList xnl = xmldoc.SelectSingleNode("myKeySet").ChildNodes;
                foreach (XmlNode xn in xnl)
                {
                    XmlElement xe = (XmlElement)xn;
                    str += xe.GetAttribute("程序名称")+"&";
                }
                return str;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return "";
            }     
        }

        //查找按键配置信息
        //返回该应用程序的所有按键配置信息
        public string findKeyValue(string fileName, string appName)
        {
            try
            {
                string str = "";
                string dirpath = Application.StartupPath + "\\data\\";
                fileName = dirpath + fileName;
                xmldoc.Load(fileName);
                XmlNodeList xnl = xmldoc.SelectSingleNode("myKeySet").ChildNodes;
                foreach (XmlNode xn in xnl)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.GetAttribute("程序名称") == appName)
                    {
                        XmlNodeList nls = xe.ChildNodes;
                        foreach (XmlNode xn1 in nls)
                        {
                            XmlElement xe2 = (XmlElement)xn1;
                            if (xe2 == null)    //判断是否为空
                            {
                                str += "*&";
                            }
                            else
                            {
                                str += xe2.InnerText + "&";
                            }
                        }
                    }
                }
                return str;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return "";
            }
        }

        //查找结点地址属性
        //返回某个程序的的地址
        public string findAppAddress(string fileName, string appName)
        {
            try
            {
                string str = "";
                string dirpath = Application.StartupPath + "\\data\\";
                fileName = dirpath + fileName;
                xmldoc.Load(fileName);
                XmlNodeList xnl = xmldoc.SelectSingleNode("myKeySet").ChildNodes;
                foreach (XmlNode xn in xnl)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.GetAttribute("程序名称") == appName)
                    {
                        str = xe.GetAttribute("程序路径");
                        break;
                    }
                }
                return str;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return "";
            }    
        }

        //读取xml文件信息
        //以dataset形式返回xml文件所有信息
        public DataSet readXml(string fileName)
        {
            try
            {
                string dirpath = Application.StartupPath + "\\data\\";
                fileName = dirpath + fileName;
                DataSet ds = new DataSet();
                ds.ReadXml(fileName);
                return ds;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }
    }
}
